package com.zgroup.controllers.admin

import com.zgroup.common.CheckUtils

class ScaffoldController extends AdminDispatchController {

  def scaffoldService
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  def beforeInterceptor = [
          action: this.&auth
  ]

  def auth() {
    return super.auth()
  }

  def list = {

    def view = params.view
    def forward = params.forward
    if (CheckUtils.isEmpty(view)) {
      view = "/${params.property.toLowerCase()}/list"
    } else {
      view = "/${params.property.toLowerCase()}/" + view;
    }
    this.println(view)
    render(view: view, model: [list: scaffoldService.paginateList(params), forward: forward])
  }

  def create = {

  }

  def save = {
    if (scaffoldService.save(params)) {
      render "1"
    } else {
      render "0"
    }
  }

  def show = {
    def view = params.view
    def forward = params.forward
    if (CheckUtils.isEmpty(view)) {
      view = "/${params.property.toLowerCase()}/show"
    }

    render(view: view, model: [bo: scaffoldService.getProperty(params), forward: forward])
  }

  def edit = {

  }

  def update = {

  }

  def remove = {
    scaffoldService.remove(params)
    return list();
  }
}
